package com.star.star.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.common.utils.Json;
import com.star.mentor.dto.StarAdDTO;
import com.star.star.entity.StarAd;
import com.star.star.enums.ArtAdPosition;
import com.star.star.mapper.StarAdMapper;
import com.star.star.service.StarAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 艺人推广 服务实现类
 *
 * @author yangzj
 */
@Service
public class StarAdServiceImpl extends ServiceImpl<StarAdMapper, StarAd> implements StarAdService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(StarAdDTO dto) {
        // 先删了
        this.lambdaUpdate().eq(StarAd::getStarId, dto.getStarId()).remove();
        // 再加
        List<StarAd> list = new ArrayList<>();
        // 原型设计不合理，需要解析数据 一个位置一条数据背景图存一样的
        String backgroundUrl = Optional.ofNullable(dto.getDetailPage()).map(StarAdDTO.DetailPage::getBackgroundUrl).orElse(null);
        StarAdDTO.HomePage homePage = dto.getHomePage();
        Long starId = dto.getStarId();
        if (homePage != null && CollUtil.isNotEmpty(homePage.getPositions())) {
            Assert.notNull(homePage.getIsPublish(), "是否首页推广不能为空");
            list.add(new StarAd().setStarId(starId).setPosition(ArtAdPosition.HOME.getCode())
                    .setBackgroundUrl(backgroundUrl).setData(Json.toString(homePage.getPositions()))
                    .setIsPublish(homePage.getIsPublish()));
        }
        StarAdDTO.InnerPage innerPage = dto.getInnerPage();
        if (innerPage != null && CollUtil.isNotEmpty(innerPage.getPositions())) {
            Assert.notNull(innerPage.getPositionSelect(), "内页推广的位置选择不能为空");
            // positionSelect目前详情好像是单选，至少从原型来看是的，值和枚举ArtAdPosition一致
            Integer positionSelect = innerPage.getPositionSelect();
            list.add(new StarAd().setStarId(starId).setPosition(positionSelect)
                    .setBackgroundUrl(backgroundUrl).setData(Json.toString(innerPage.getPositions()))
                    .setIsPublish(innerPage.getPositionSelect() > 0));
        }
        this.saveBatch(list);
    }

    @Override
    public StarAdDTO getDetail(Long starId) {
        List<StarAd> list = this.list(Wrappers.<StarAd>lambdaQuery().eq(StarAd::getStarId, starId));
        if (list.isEmpty()) {
            return null;
        }
        StarAdDTO dto = new StarAdDTO();
        String backgroundUrl = list.stream().map(StarAd::getBackgroundUrl).filter(StrUtil::isNotBlank).findFirst().orElse(null);
        StarAd home = list.stream().filter(c -> ArtAdPosition.HOME.getCode().equals(c.getPosition())).findFirst().orElse(null);
        StarAd inner = list.stream()
                .filter(c -> ArtAdPosition.NEWS_LIST.getCode().equals(c.getPosition()) || ArtAdPosition.NEWS_DETAIL.getCode().equals(c.getPosition()))
                .findFirst().orElse(null);
        return dto.setStarId(starId)
                .setDetailPage(new StarAdDTO.DetailPage().setBackgroundUrl(backgroundUrl))
                .setHomePage(home == null ? null : new StarAdDTO.HomePage().setIsPublish(home.getIsPublish()).setPositions(Json.toList(home.getData())))
                .setInnerPage(inner == null ? null : new StarAdDTO.InnerPage().setPositionSelect(inner.getPosition()).setPositions(Json.toList(inner.getData())));
    }
}
